package com.dingteam.question.common.validation;

import com.dingteam.question.common.enums.ModuleEnum;
import com.dingteam.question.common.validation.constraints.ModuleType;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * 控件类型的校验实现
 * @author Jamin
 * @date 2019/06/27 13:49:55
 */
public class ModuleTypeValidator implements ConstraintValidator<ModuleType, CharSequence> {

	@Override
	public void initialize(ModuleType constraintAnnotation) {
	}

	@Override
	public boolean isValid(CharSequence value, ConstraintValidatorContext context) {
		if (value == null || value.length() == 0) {
			return true;
		}
		try {
			ModuleEnum.valueOfAlias(value.toString());
			return true;
		} catch (Exception e) {
			return false;
		}
	}
}
